using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class InputManager : MonoBehaviour
{
    private SerialController serialController;
    public static InputManager instance;

    public bool keyA;
    public bool keyB;

    public bool keyADown;
    public bool keyBDown;

    public UnityEvent keyADownEvent;
    public UnityEvent keyBDownEvent;

    private bool restartDown;

    void Start()
    {
        if (GameManager.instance.IsUsingController)
            serialController = GetComponent<SerialController>();
        keyADown = false;
        keyBDown = false;
        restartDown = false;
    }

    private void Update()
    {
        string a;
        if (GameManager.instance.IsUsingController)
        {
            a = serialController.ReadSerialMessage();
            //Debug.Log(a);
        }
        else
            a = "";
        if (Input.GetKeyDown(KeyCode.Space) || (a == "0"))
        {
            keyA = true;
            keyADown = true;
        }
        if (Input.GetKeyUp(KeyCode.Space) || (a == "1"))
        {
            keyA = false;
        }
        if (Input.GetKeyDown(KeyCode.F) || (a == "2"))
        {
            keyB = true;
            keyBDown = true;
        }
        if (Input.GetKeyUp(KeyCode.F) || (a == "3"))
        {
            keyB = false;
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            restartDown = true;
        }
    }

    private void FixedUpdate()
    {
        if (keyADown)
        {
            keyADownEvent.Invoke();
            keyADown = false;
        }
        if (keyBDown)
        {
            keyBDownEvent.Invoke();
            keyBDown = false;
        }
        if (restartDown)
        {
            GameManager.instance.GameEnd();
            restartDown = false;
        }
    }
}
